//
//  CYInvestSucceededTopCell.m
//  cyjf
//
//  Created by tauCross on 2017/5/9.
//  Copyright © 2017年 Auto Financing. All rights reserved.
//

#import "CYInvestSucceededTopCell.h"

#import <TTTAttributedLabel.h>



@implementation CYInvestSucceededTopCellObject

@end



@interface CYInvestSucceededTopCell ()

@property(nonatomic, strong)UIImageView *succeededImageView;
@property(nonatomic, strong)TTTAttributedLabel *succeededLabel;

@property(nonatomic, strong)CYInvestSucceededTopCellObject *object;

@end


@implementation CYInvestSucceededTopCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self)
    {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor clearColor];
        self.contentView.backgroundColor = [UIColor whiteColor];
        
        self.succeededImageView = [[UIImageView alloc] initWithImage:CYImage(@"invest_succeeded")];
        [self.contentView addSubview:self.succeededImageView];
        
        @weakify(self);
        NSString *textString = @"您的资金现已进入第三方支付银行备用金账户，后续资金流转详情，可查看";
        NSString *linkString = @"投资详情";
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@%@", textString, linkString] attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:14], NSForegroundColorAttributeName : HEXCOLOR(@"666666")}];
        TTTAttributedLabelLink *link = [[TTTAttributedLabelLink alloc] initWithAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:14], NSForegroundColorAttributeName : APP_DEFAULT_COLOR_BLUE} activeAttributes:nil inactiveAttributes:nil textCheckingResult:[NSTextCheckingResult linkCheckingResultWithRange:NSMakeRange(textString.length, linkString.length) URL:@"".tc_url]];
        link.linkTapBlock = ^(TTTAttributedLabel *label, TTTAttributedLabelLink *link_) {
            @strongify(self);
            [self onDetailAction];
        };
        self.succeededLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
        self.succeededLabel.numberOfLines = 0;
        self.succeededLabel.text = attributedString;
        [self.succeededLabel addLink:link];
        [self.contentView addSubview:self.succeededLabel];
    }
    return self;
}

+ (CGFloat)heightForObject:(id)object atIndexPath:(NSIndexPath *)indexPath tableView:(UITableView *)tableView
{
    return 80;
}

- (BOOL)shouldUpdateCellWithObject:(id)object
{
    self.object = object;
    
    [self setNeedsLayout];
    return YES;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.succeededImageView.right = self.contentView.width;
    self.succeededImageView.bottom = self.contentView.height;
    
    self.succeededLabel.size = [self.succeededLabel sizeThatFits:CGSizeMake(self.contentView.width - (SCREEN_WIDTH == 320 ? 40 : 80), MAXFLOAT)];
    self.succeededLabel.centerX = self.contentView.width / 2;
    self.succeededLabel.centerY = self.contentView.height / 2;
}

#pragma mark - Actions
- (void)onDetailAction
{
    if(self.object.detailAction)
    {
        self.object.detailAction();
    }
}

@end
